
package com.platform.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * https://github.com/qcloudsms/qcloudsms_java/blob/master/src/test/java/QcloudSmsTest.java
 *
 * @author 
 * @date 2017年11月18日 下午13:13:23
 */
public class SmsUtil {

    /**
     * 指定模板ID群发
     * 签名参数未提供或者为空时，会使用默认签名发送短信
     *
     * @param appid        appid
     * @param appkey       appkey
     * @param phoneNumbers 手机号
     * @param templateId   模板ID
     * @param params       参数
     * @param sign         签名
     * @return
     */
    public static SmsMultiSenderResult crSendSms(int appid, String appkey, String[] phoneNumbers, int templateId, String[] params, String sign) {
        SmsMultiSenderResult result = new SmsMultiSenderResult();
        try {
            SmsMultiSender msender = new SmsMultiSender(appid, appkey);
            result = msender.sendWithParam("86", phoneNumbers,
                    templateId, params, sign, "", "");
        } catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return result;
    }
    public static byte[] inputStream2Bytes(InputStream stream) throws IOException {
        byte[] bytes = new byte[0];
        if (stream != null) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int ch;
            while ((ch = stream.read()) != -1) {
                outputStream.write(ch);
            }
            bytes = outputStream.toByteArray();
            outputStream.close();
        }
        return bytes;
    }
    public static String umsSendSms(String SpCode,String LoginName, String Password, String MessageContent, String UserNumber, String templateId,
                                    String SerialNumber,Boolean isEnable ,String proxyIp ,Integer proxyPort ,String template){
        String info = null;
        try{
//            String content = "欢迎使用本行金融权益平台，您本次验证码为"+MessageContent+"，请保护好个人隐私信息。";
//            String content = StringUtils.smsTemplate(template, replace,MessageContent, values);
            String content = StringUtils.smsTemplate(template, MessageContent);
//            System.out.println("发送内容："+content);
//            String content = MessageContent;
                    String oldUnicode = new String(content.getBytes("UTF-8"), "UTF-8");
            String gbkStr = new String(oldUnicode.getBytes("GBK"),"GBK");

            HttpClient httpclient = new HttpClient();
            if(isEnable){
                httpclient.getHostConfiguration().setProxy(proxyIp,proxyPort);
            }
            PostMethod post = new PostMethod("https://api.ums86.com:9600/sms/Api/Send.do");//
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");
            post.addParameter("SpCode", SpCode);
            post.addParameter("LoginName", LoginName);
            post.addParameter("Password", Password);
            post.addParameter("MessageContent", gbkStr);
            post.addParameter("UserNumber", UserNumber);
            post.addParameter("SerialNumber", SerialNumber);
            post.addParameter("ScheduleTime", "");
            post.addParameter("f", "1");
            post.addParameter("templateId", templateId);

            httpclient.executeMethod(post);
            info = new String(post.getResponseBody(),"gbk");
        }catch (Exception e) {
            e.printStackTrace();
        }
        String replaces = "{\""+info.replace("&", "\",\"").replace("=","\":\"")+"\"}";

        return  replaces;

    }

    /**
     * 指定模板ID单发短信
     * 签名参数未提供或者为空时，会使用默认签名发送短信
     *
     * @param appid       appid
     * @param appkey      appkey
     * @param phoneNumber 手机号
     * @param templateId
     * @param params
     * @param smsSign
     * @return
     */
    public static SmsSingleSenderResult crSendSms(int appid, String appkey, String phoneNumber, int templateId, String[] params, String smsSign) {
        SmsSingleSenderResult result = new SmsSingleSenderResult();
        try {
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            result = ssender.sendWithParam("86", phoneNumber,
                    templateId, params, smsSign, "", "");
        } catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 单发短信
     *
     * @param appid       appid
     * @param appkey      appkey
     * @param phoneNumber 手机号
     * @param msg         短信内容
     * @return
     */
    public static SmsSingleSenderResult crSendSms(int appid, String appkey, String phoneNumber, String msg) {
        SmsSingleSenderResult result = new SmsSingleSenderResult();
        try {
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            result = ssender.send(0, "86", phoneNumber, msg, "", "");
        } catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 群发短信
     *
     * @param appid        appid
     * @param appkey       appkey
     * @param phoneNumbers 手机号
     * @param msg          短信内容
     * @return
     */
    public static SmsMultiSenderResult crSendSms(int appid, String appkey, String[] phoneNumbers, String msg) {
        SmsMultiSenderResult result = new SmsMultiSenderResult();
        try {
            SmsMultiSender msender = new SmsMultiSender(appid, appkey);
            result = msender.send(0, "86", phoneNumbers, msg, "", "");
        } catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return result;
    }
}
